/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package titanisu.basicMathModule1.DecimalNumberMultiplication;

import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import titanisu.commonUtil.MathTextQuestionsUI;

/**
 *
 * @author Balaji
 */
public class ExamController implements titanisu.Modules.baseInterfaces.moduleExamController {

    ArrayList quests = new ArrayList();
    int numberOfQuestions, timeOfQuestions, timeOfAnswer;
int opt1=-1;

    @Override
    public boolean startExam() {


        QuestionGeneratorAddSub qe = new QuestionGeneratorAddSub();

//        titanisu.commonUtil.MathsSmallQuestion6Sets msq6 = new titanisu.commonUtil.MathsSmallQuestion6Sets();
        MathTextQuestionsUI msq6 = new titanisu.commonUtil.MathTextQuestionsUI();

        msq6.setVisible(true);
        opt1 = JOptionPane.showConfirmDialog(msq6, "Do you want to include negetive integers in questions ?");
        System.out.println("opt1 = " + opt1);
        if (opt1 == 0) {
            qe.setIntegers(true);
        } else {
            qe.setIntegers(false);
        }


        numberOfQuestions = Integer.parseInt(JOptionPane.showInputDialog(" enter number of questions"));
        timeOfQuestions = Integer.parseInt(JOptionPane.showInputDialog(" enter time of questions"));
        timeOfAnswer = Integer.parseInt(JOptionPane.showInputDialog("Enter time of answer."));
        timeOfQuestions *= 1000;
timeOfAnswer *= 1000;
        for (int i = 0; i < numberOfQuestions; i++) {
            presentQuestions(qe, msq6, i);
        }

        System.out.println("Display of Questions s done");

        int opt = -1;
        while (opt != 0) {
            opt = JOptionPane.showConfirmDialog(msq6, "Do you want to continue to answers ?");
            System.out.println("opt = " + opt);
        }



        for (int i = 0; i < numberOfQuestions; i++) {
            presentAnswers(qe, msq6, i);

        }
        System.out.println("Display of Answers done");

        JOptionPane.showMessageDialog(msq6, "Time's Up !!!");

        return true;

    }

    private void presentQuestions(QuestionGeneratorAddSub qe, MathTextQuestionsUI msq6, int i) {
        try {
            ArrayList arlst = new ArrayList();
            long start = new Date().getTime();
            ArrayList questAL = new ArrayList();
            for (int j = 0; j < 6; j++) {
                QuestionGeneratorAddSub qeTemp = new QuestionGeneratorAddSub();
                if (opt1 == 0) {
                    qeTemp.setIntegers(true);
                } else {
                    qeTemp.setIntegers(false);
                }
                String temp = qeTemp.getQuestions();
                questAL.add(j, qeTemp.getQuest());
//                System.out.println("" + temp);
                arlst.add(j, temp);

            }
            msq6.setQuestions(arlst);

            quests.add(i, questAL);
//            questAL.clear();

            arlst.clear();

            long end = new Date().getTime();
            long consumedTime = end - start;

            Thread.sleep(i);
            if (consumedTime < timeOfQuestions) {
                Thread.sleep(timeOfQuestions - consumedTime);
            } else {
                System.out.println("Delayed, time taken more than 2 seconds ");
                Thread.sleep(timeOfQuestions);
            }

        } catch (InterruptedException ex) {
            Logger.getLogger(ExamController.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println("Questions presentation is done");
    }

    private void presentAnswers(QuestionGeneratorAddSub qe, MathTextQuestionsUI msq6, int i) {
        try {
            ArrayList arlst = new ArrayList();
            ArrayList arlstQ = new ArrayList();
            long start = new Date().getTime();
            ArrayList questAL;
            questAL = (ArrayList) quests.get(i);
            System.out.println("quests.size() = " + quests.size());
            System.out.println("questAL.size() = " + questAL.size());

            for (int j = 0; j < 6; j++) {
                qe.setQuest((ArrayList) questAL.get(j));
                ArrayList tempAL = (ArrayList) questAL.get(j);
                String temp = (String) tempAL.get(1);
                String tempQ = (String) tempAL.get(0);

                arlstQ.add(j, tempQ);
                arlst.add(j, temp);
            }

            msq6.setQuestions(arlstQ);
            msq6.setAnswers(arlst);

            questAL.clear();
            arlst.clear();

            long end = new Date().getTime();
            long consumedTime = end - start;

            Thread.sleep(i);
            if (consumedTime < timeOfAnswer) {
                Thread.sleep(timeOfAnswer - consumedTime);
            } else {
                System.out.println("Delayed, time taken more than 2 seconds for question generation");
                Thread.sleep(timeOfAnswer);
            }

        } catch (InterruptedException ex) {
            Logger.getLogger(ExamController.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Answers presentation is done");
    }
}
